$(function () {
    var icon = "<i class='fa fa-times-circle'></i>";
    $("#signupForm").validate({
        rules: {
            username: {
                required: true,
                minlength: 2,
                maxlength: 20
            },
            password: {
                required: true,
                minlength: 6,
                maxlength: 20
            }
        },
        messages: {
            username: {
                required: icon + "请输入您的用户名",
                minlength: icon + "用户名不能小于2个字符",
                maxlength: icon + "用户名不能大于20个字符"
            },
            password: {
                required: icon + "请输入您的密码",
                minlength: icon + "密码不能小于6个字符",
                maxlength: icon + "密码不能大于20个字符"
            }
        },
        submitHandler: function (form) {
            login();
        }
    })
})

function login() {
    var data = {
        "loginName": $("input[name='username']").val(),
        "loginPassword": $("input[name='password']").val()
    }
    postAjax("/backend/login", JSON.stringify(data), function (response) {
        if (response.code == 0) {
            window.location.replace(`/backend/index?token=${encodeURIComponent(response.data.token)}`);
            cocoMessage.success("登录成功！")
            localStorage.setItem("token", response.data.token)
            return;
        }
        if (response.code == 500) {
            cocoMessage.error("用户名或密码错误！！！")
            return;
        }
    })
}
